package mercurius.fitness.client.rb.measurement;

import java.util.HashMap;
import java.util.Vector;

import mercurius.fitness.client.util.MercuriusHelper;
import mercurius.fitness.client.util.AbstractResponseCommand;
import net.pleso.framework.client.dal.SelectParams;
import net.pleso.framework.client.util.DateUtil;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class MeasurementsDataSet {
	
	public MeasurementsDataSet() {

	}

	public void select(final SelectParams params, final AsyncCallback callback) {
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {
				   
			   }

			public Object getObject() {
				return params;
			}

			public void setObject(Object obj) {
				Vector props = (Vector) obj;
				Measurements[] measurements = new Measurements[props.size()];
				for(int i=0;i<props.size();i++) {
					HashMap measurement = (HashMap)props.get(i);
					measurements[i] = new Measurements(
							
							new Integer(Integer.parseInt((String) measurement.get("id"))),
							new Float(Float.parseFloat((String) measurement.get("weight"))),
							new Float(Float.parseFloat((String) measurement.get("fat"))),
							DateUtil.parseDate((String) measurement.get("date")),
							(String) measurement.get("notes"),
							new Integer(Integer.parseInt((String) measurement.get("restingpulse"))),
							(String) measurement.get("milestone")
							
					);

				}
				//	Arrays.sort(measurements, new DataRowComparator(params.getOrderByColumnName(), params.getOrderByDirection()));
					callback.onSuccess(measurements);
			}
			 };
			
			MercuriusHelper.updateMercurius(new Measurements(),MercuriusHelper.MERCURIUS_SELECT,callback,pass);
	}
	
	public void selectCount(AsyncCallback callback) {
		MercuriusHelper.updateMercurius(new Measurements(),MercuriusHelper.MERCURIUS_SELECT_COUNT,callback);
	}
	
	public void add(final Measurements measurement, AsyncCallback callback) {
			AbstractResponseCommand pass = new AbstractResponseCommand() {
			   public void execute() {
				   measurement.setID(new Integer(results));
			   }

			public Object getObject() {
				return null;
			}

			public void setObject(Object obj) {
				
			}
			 };
			
			MercuriusHelper.updateMercurius(measurement,MercuriusHelper.MERCURIUS_INSERT,callback,pass);
	}
	
	public void update(final Measurements measurement, AsyncCallback callback) {
		MercuriusHelper.updateMercurius(measurement,MercuriusHelper.MERCURIUS_UPDATE,callback);
	}
	
	public void get(final Integer id, final AsyncCallback callback)
	{
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {
				   
			   }

			public Object getObject() {
				return id;
			}

			public void setObject(Object obj) {
				
				
				HashMap m = (HashMap) obj;
				Measurements measurement = new Measurements(
						
						new Integer(Integer.parseInt((String) m.get("id"))),
						new Float(Float.parseFloat((String) m.get("weight"))),
						new Float(Float.parseFloat((String) m.get("fat"))),
						DateUtil.parseDate((String) m.get("date")),
						(String) m.get("notes"),
						new Integer(Integer.parseInt((String) m.get("restingpulse"))),
						(String) m.get("milestone")
						
				);

				callback.onSuccess(measurement);
			}
			 };
			 
			 MercuriusHelper.updateMercurius(new Measurements(),MercuriusHelper.MERCURIUS_SELECT_BY_ID,callback,pass);

	}
	
	public void delete(Integer id, AsyncCallback callback)
	{
		Measurements measurement = new Measurements();
		measurement.setID(id);
		MercuriusHelper.updateMercurius(measurement,MercuriusHelper.MERCURIUS_DELETE,callback);
	}
}
